package com.xexun.gpstracker.util;

import java.util.Collection;
import java.util.LinkedList;

/**
 * Created by HH
 * Date: 2015/11/21 0021
 * Time: 下午 4:36
 */
public class FixedLengthList<T> extends LinkedList<T> {
    private int maxSize = 8;

    public FixedLengthList() {
    }

    public FixedLengthList(int maxSize) {
        this.maxSize = maxSize;
    }

    @Override
    public boolean add(T object) {
        while (size() >= maxSize) {
            poll();
        }
        return super.add(object);
    }

    @Override
    public boolean addAll(Collection<? extends T> collection) {
        if (collection.size() > maxSize) {
            return false;
        }
        int newSize = size() + collection.size();
        removeRange(0, newSize - maxSize);
        return super.addAll(collection);
    }
}
